const express = require('express')
const router = express.Router()
const db = require('../db')
const consts = require('../../consts/Consts')
/**
 * @apiDefine task web任务管理
 */

/**
 * @api {get} http://127.0.0.1:3658/task/getSetTaskList 获取设置航标任务列表
 * @apiDescription 获取设置任务列表
 * @apiName getSetTaskList
 * @apiGroup task
 *
 * @apiSuccess {int} code 状态码.
 * @apiSuccess {String} data  json数据.
 * @apiSuccessExample {json} Success-Response:
 *  {
 *      "code" : 20000,
 *      "data" : {}
 *
 *  }
 * @apiVersion 1.0.0
 */
router.get('/getSetTaskList', (req, res, err) => {
  console.log('task-getSetTaskList: ')
  db.TaskDb.getTaskList(consts.TASK_TYPE_SET_MARK, (err, list) => {
    if (err) throw err
    res.send({
      code: consts.RES_OK,
      data: list
    })
  })
})
/**
 * @api {get} http://127.0.0.1:3658/task/getChangeTaskList 获取修改航标任务列表
 * @apiDescription 获取修改航标任务列表
 * @apiName getChangeTaskList
 * @apiGroup task
 *
 * @apiSuccess {int} code 状态码.
 * @apiSuccess {String} data  json数据.
 * @apiSuccessExample {json} Success-Response:
 *  {
 *      "code" : 20000,
 *      "data" : {}
 *
 *  }
 * @apiVersion 1.0.0
 */
router.get('/getChangeTaskList', (req, res, err) => {
  console.log('task-getChangeTaskList: ')
  db.TaskDb.getTaskList(consts.TASK_TYPE_CHANGE_MARK, (err, list) => {
    if (err) throw err
    res.send({
      code: consts.RES_OK,
      data: list
    })
  })
})
/**
 * @api {get} http://127.0.0.1:3658/task/getRetestTaskList 获取位置复测任务列表
 * @apiDescription 获取位置复测任务列表
 * @apiName getRetestTaskList
 * @apiGroup task
 *
 * @apiSuccess {int} code 状态码.
 * @apiSuccess {String} data  json数据.
 * @apiSuccessExample {json} Success-Response:
 *  {
 *      "code" : 20000,
 *      "data" : {}
 *
 *  }
 * @apiVersion 1.0.0
 */
router.get('/getRetestTaskList', (req, res, err) => {
  console.log('task-getRetestTaskList: ')
  db.TaskDb.getTaskList(consts.TASK_TYPE_RETEST, (err, list) => {
    if (err) throw err
    res.send({
      code: consts.RES_OK,
      data: list
    })
  })
})
/**
 * @api {get} http://127.0.0.1:3658/task/getTechMeasureTaskList 获取位置测定任务列表
 * @apiDescription 获取位置测定任务列表
 * @apiName getTechMeasureTaskList
 * @apiGroup task
 *
 * @apiSuccess {int} code 状态码.
 * @apiSuccess {String} data  json数据.
 * @apiSuccessExample {json} Success-Response:
 *  {
 *      "code" : 20000,
 *      "data" : {}
 *
 *  }
 * @apiVersion 1.0.0
 */
router.get('/getTechMeasureTaskList', (req, res, err) => {
  db.TaskDb.getTaskList(consts.TASK_TYPE_TECH, (err, list) => {
    if (err) throw err
    res.send({
      code: consts.RES_OK,
      data: list
    })
  })
})
/**
 * @api {get} http://127.0.0.1:3658/task/getMaintainTaskList 获取应急维修任务列表
 * @apiDescription 获取应急维修任务列表
 * @apiName getMaintainTaskList
 * @apiGroup task
 *
 * @apiSuccess {int} code 状态码.
 * @apiSuccess {String} data  json数据.
 * @apiSuccessExample {json} Success-Response:
 *  {
 *      "code" : 20000,
 *      "data" : {}
 *
 *  }
 * @apiVersion 1.0.0
 */
router.get('/getMaintainTaskList', (req, res, err) => {
  db.TaskDb.getTaskList(consts.TASK_TYPE_MAINTAIN_MARK, (err, list) => {
    if (err) throw err
    res.send({
      code: consts.RES_OK,
      data: list
    })
  })
})
/**
 * @api {get} http://127.0.0.1:3658/task/getRemoveTaskList 获取航标移除任务列表
 * @apiDescription 获取航标移除任务列表
 * @apiName getRemoveTaskList
 * @apiGroup task
 *
 * @apiSuccess {int} code 状态码.
 * @apiSuccess {String} data  json数据.
 * @apiSuccessExample {json} Success-Response:
 *  {
 *      "code" : 20000,
 *      "data" : {}
 *
 *  }
 * @apiVersion 1.0.0
 */
router.get('/getRemoveTaskList', (req, res, err) => {
  db.TaskDb.getTaskList(consts.TASK_TYPE_REMOVE_MARK, (err, list) => {
    if (err) throw err
    res.send({
      code: consts.RES_OK,
      data: list
    })
  })
})
/**
 * @api {get} http://127.0.0.1:3658/task/getCheckTaskList 获取航标巡检任务列表
 * @apiDescription 获取航标巡检任务列表
 * @apiName getCheckTaskList
 * @apiGroup task
 *
 * @apiSuccess {int} code 状态码.
 * @apiSuccess {String} data  json数据.
 * @apiSuccessExample {json} Success-Response:
 *  {
 *      "code" : 20000,
 *      "data" : {}
 *
 *  }
 * @apiVersion 1.0.0
 */
router.get('/getCheckTaskList', (req, res, err) => {
  db.TaskDb.getTaskList(consts.TASK_TYPE_CHECK_MARK, (err, list) => {
    if (err) throw err
    res.send({
      code: consts.RES_OK,
      data: list
    })
  })
})
/**
 * @api {post} http://127.0.0.1:3658/task/addSetTask 添加设置航标任务
 * @apiDescription 添加设置航标任务
 * @apiName addSetTask
 * @apiGroup task
 *
 * @apiSuccess {int} code 状态码.
 * @apiSuccess {String} data  json数据.
 * @apiSuccessExample {json} Success-Response:
 *  {
 *      "code" : 20000,
 *      "data" : {}
 *
 *  }
 * @apiVersion 1.0.0
 */
router.post('/addSetTask', (req, res, err) => {
  console.log('task-addSetTask: ')
  db.MarkDb.addMark(req.body, (err, row) => {
    if (err) {
      console.log('addmark error:' + err)
    } else {
      console.log('addmark row:%o', row)
    }
  })
  db.TaskDb.addSetTask(req.body, (err, row) => {
    if (err) {
      console.log('addSetTask error:' + err)
      throw err
    }
    res.send({
      code: consts.RES_OK,
      data: {}
    })
  })
})


/**
 * @api {post} http://127.0.0.1:3658/task/addChangeTask 添加修改航标任务
 * @apiDescription 添加修改航标任务
 * @apiName addChangeTask
 * @apiGroup task
 *
 * @apiSuccess {int} code 状态码.
 * @apiSuccess {String} data  json数据.
 * @apiSuccessExample {json} Success-Response:
 *  {
 *      "code" : 20000,
 *      "data" : {}
 *
 *  }
 * @apiVersion 1.0.0
 */
router.post('/addChangeTask', (req, res, err) => {
  console.log('task-addSetTask: ')
  db.TaskDb.addChangeTask(req.body, (err, row) => {
    if (err) throw err
    res.send({
      code: consts.RES_OK,
      data: {}
    })
  })
})



/**
 * @api {post} http://127.0.0.1:3658/task/addTechMeasureTask 添加位置测定任务
 * @apiDescription 添加位置测定任务
 * @apiName addTechMeasureTask
 * @apiGroup task
 *
 * @apiSuccess {int} code 状态码.
 * @apiSuccess {String} data  json数据.
 * @apiSuccessExample {json} Success-Response:
 *  {
 *      "code" : 20000,
 *      "data" : {}
 *
 *  }
 * @apiVersion 1.0.0
 */
router.post('/addTechMeasureTask', (req, res, err) => {
  db.TaskDb.addTechMeasureTask(req.body, (err, row) => {
    if (err) throw err
    res.send({
      code: consts.RES_OK,
      data: {}
    })
  })
})


/**
 * @api {post} http://127.0.0.1:3658/task/addMaintainTask 添加应急维修任务
 * @apiDescription 添加应急维修任务
 * @apiName addMaintainTask
 * @apiGroup task
 *
 * @apiSuccess {int} code 状态码.
 * @apiSuccess {String} data  json数据.
 * @apiSuccessExample {json} Success-Response:
 *  {
 *      "code" : 20000,
 *      "data" : {}
 *
 *  }
 * @apiVersion 1.0.0
 */
router.post('/addMaintainTask', (req, res, err) => {
  db.TaskDb.addMaintainTask(req.body, (err, row) => {
    if (err) throw err
    res.send({
      code: consts.RES_OK,
      data: {}
    })
  })
})


/**
 * @api {post} http://127.0.0.1:3658/task/addRemoveTask 添加移除航标任务
 * @apiDescription 添加移除航标任务
 * @apiName addRemoveTask
 * @apiGroup task
 *
 * @apiSuccess {int} code 状态码.
 * @apiSuccess {String} data  json数据.
 * @apiSuccessExample {json} Success-Response:
 *  {
 *      "code" : 20000,
 *      "data" : {}
 *
 *  }
 * @apiVersion 1.0.0
 */
router.post('/addRemoveTask', (req, res, err) => {
  db.TaskDb.addRemoveTask(req.body, (err, row) => {
    if (err) throw err
    res.send({
      code: consts.RES_OK,
      data: {}
    })
  })
})

/**
 * @api {post} http://127.0.0.1:3658/task/addCheckTask 添加航标巡检任务
 * @apiDescription 添加航标巡检任务
 * @apiName addCheckTask
 * @apiGroup task
 *
 * @apiSuccess {int} code 状态码.
 * @apiSuccess {String} data  json数据.
 * @apiSuccessExample {json} Success-Response:
 *  {
 *      "code" : 20000,
 *      "data" : {}
 *
 *  }
 * @apiVersion 1.0.0
 */
router.post('/addCheckTask', (req, res, err) => {
  db.TaskDb.addCheckTask(req.body, (err, row) => {
    if (err) throw err
    res.send({
      code: consts.RES_OK,
      data: {}
    })
  })
})
/**
 * @api {post} http://127.0.0.1:3658/task/addRetestTask 添加位置复测任务
 * @apiDescription 添加位置复测任务
 * @apiName addRetestTask
 * @apiGroup task
 *
 * @apiSuccess {int} code 状态码.
 * @apiSuccess {String} data  json数据.
 * @apiSuccessExample {json} Success-Response:
 *  {
 *      "code" : 20000,
 *      "data" : {}
 *
 *  }
 * @apiVersion 1.0.0
 */
router.post('/addRetestTask', (req, res, err) => {
  db.TaskDb.addRetestTask(req.body, (err, row) => {
    if (err) throw err
    res.send({
      code: consts.RES_OK,
      data: {}
    })
  })
})
/**
 * @api {post} http://127.0.0.1:3658/task/delTask 删除任务
 * @apiDescription 删除任务
 * @apiName delTask
 * @apiGroup task
 *
 * @apiParam {String} Code 任务id
 * @apiSuccess {int} code 状态码.
 * @apiSuccess {String} data  json数据.
 * @apiSuccessExample {json} Success-Response:
 *  {
 *      "code" : 20000,
 *      "data" : {}
 *
 *  }
 * @apiVersion 1.0.0
 */
router.get('/delTask', (req, res) => {
  const code = req.query.Code
  console.log('deleteTask: %s', code)
  db.TaskDb.delTask(code, (err, row) => {
    if (err) throw err
    console.log('row: %o', row)
    res.send({
      code: consts.RES_OK,
      data: 'ok'
    })
  })
})
/**
 * @api {post} http://127.0.0.1:3658/task/getCount 获取各类任务数量
 * @apiDescription 获取各类任务数量
 * @apiName getCount
 * @apiGroup task
 *
 * @apiSuccess {int} code 状态码.
 * @apiSuccess {String} data  json数据.
 * @apiSuccessExample {json} Success-Response:
 *  {
 *      "code" : 20000,
 *      "data" : {}
 *
 *  }
 * @apiVersion 1.0.0
 */
router.get('/getCount', (req, res, err) => {
  console.log('mark-getCount: ')
  let PromiseArr = Array(8)
  PromiseArr[0] = new Promise((resolve, reject) => {
    db.TaskDb.getTaskCount(consts.TASK_TYPE_SET_MARK, (err, result) => {
      if (err) {
        reject(err)
      } else {
        resolve(result)
      }
    })
  })
  PromiseArr[1] = new Promise((resolve, reject) => {
    db.TaskDb.getTaskCount(consts.TASK_TYPE_CHANGE_MARK, (err, result) => {
      if (err) {
        reject(err)
      } else {
        resolve(result)
      }
    })
  })
  PromiseArr[2] = new Promise((resolve, reject) => {
    db.TaskDb.getTaskCount(consts.TASK_TYPE_REMOVE_MARK, (err, result) => {
      if (err) {
        reject(err)
      } else {
        resolve(result)
      }
    })
  })
  PromiseArr[3] = new Promise((resolve, reject) => {
    db.TaskDb.getTaskCount(consts.TASK_TYPE_CHECK_MARK, (err, result) => {
      if (err) {
        reject(err)
      } else {
        resolve(result)
      }
    })
  })
  PromiseArr[4] = new Promise((resolve, reject) => {
    db.TaskDb.getCheckedCount((err, result) => {
      if (err) {
        reject(err)
      } else {
        resolve(result)
      }
    })
  })
  PromiseArr[5] = new Promise((resolve, reject) => {
    db.TaskDb.getUnUsualCount((err, result) => {
      if (err) {
        reject(err)
      } else {
        resolve(result)
      }
    })
  })
  PromiseArr[6] = new Promise((resolve, reject) => {
    db.TaskDb.getMaintainedCount((err, result) => {
      if (err) {
        reject(err)
      } else {
        resolve(result)
      }
    })
  })
  PromiseArr[7] = new Promise((resolve, reject) => {
    db.TaskDb.getUnMaintainedCount((err, result) => {
      if (err) {
        reject(err)
      } else {
        resolve(result)
      }
    })
  })
  Promise.all(PromiseArr).then(array => {
    console.log('cyw:%o', array)
    res.send({
      code: consts.RES_OK,
      data: array
    })
  })
})
module.exports = router
